package com.digitalchina.lxnsy.activity.old;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessHelpFeedback;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.MyFeedbackListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.JointImageUrl;
import com.lzy.ninegrid.ImageInfo;
import com.lzy.ninegrid.NineGridView;
import com.lzy.ninegrid.preview.NineGridViewClickAdapter;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2018/3/15 18:28
 * 修改备注：
 * ================================================
 */
public class FeedbackDetailActivity extends BaseActivity {

    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.layout_imgs)
    LinearLayout layoutImgs;
    @Bind(R.id.tv_re_time)
    TextView tvReTime;
    @Bind(R.id.tv_re_content)
    TextView tvReContent;
    @Bind(R.id.layout_re_imgs)
    LinearLayout layoutReImgs;
    @Bind(R.id.nineGrid)
    NineGridView nineGrid;
    @Bind(R.id.nineGrid_rep)
    NineGridView nineGridRep;

    private MyFeedbackListVO vo;

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_feedback_detail);
        setTitle("反馈详情");
    }

    @Override
    public void initMember() {
        if (getIntent().hasExtra("MyFeedbackListVO")) {
            vo = (MyFeedbackListVO) getIntent().getSerializableExtra("MyFeedbackListVO");
            setView();
        } else if (getIntent().hasExtra("noticeID")) {
            BusinessHelpFeedback.getFBInfo(this, getIntent().getSerializableExtra("noticeID").toString(), mHandler);
        } else {
            showToast("获取信息错误");
        }

    }

    private void setView() {
        if (vo == null)
            return;

        tvTime.setText(vo.getCreateTime() + "");
        tvTitle.setText(vo.getFbContent() + "");


        ArrayList<ImageInfo> imageInfo = new ArrayList<>();
        ArrayList<ImageInfo> imageInfoRep = new ArrayList<>();

        if (vo.getFeedBackImgs() != null && vo.getFeedBackImgs().size() > 0) {
            nineGrid.setVisibility(View.VISIBLE);
            for (int i = 0; i < vo.getFeedBackImgs().size(); i++) {
//                View view = LayoutInflater.from(this).inflate(R.layout.view_image, null);
//                SimpleDraweeView sdv = view.findViewById(R.id.sdv_image);
//                FrescoUtil.showImageSmall(vo.getFeedBackImgs().get(i).getImgID(), sdv);
//                layoutImgs.addView(view);
                ImageInfo info = new ImageInfo();
                info.setThumbnailUrl(JointImageUrl.smallImageUrl(vo.getFeedBackImgs().get(i).getImgID()));
                info.setBigImageUrl(JointImageUrl.bigImageUrl(vo.getFeedBackImgs().get(i).getImgID()));
                imageInfo.add(info);
            }
            nineGrid.setAdapter(new NineGridViewClickAdapter(this, imageInfo));
        }

        if (vo.getReplyStatus() == 0) { // 没有回复
            tvReTime.setVisibility(View.GONE);
            tvReContent.setText("暂无回复");
        } else { // 已经回复
            tvReTime.setText(vo.getReplyTime() + "");
            tvReContent.setText(vo.getReplyContent() + "");
            if (vo.getReplyImgs() != null && vo.getReplyImgs().size() > 0) {
                layoutReImgs.setVisibility(View.VISIBLE);
                for (int i = 0; i < vo.getReplyImgs().size(); i++) {
//                    View view = LayoutInflater.from(this).inflate(R.layout.view_image, null);
//                    SimpleDraweeView sdv = view.findViewById(R.id.sdv_image);
//                    FrescoUtil.showImageSmall(vo.getReplyImgs().get(i).getImgID(), sdv);
//                    layoutReImgs.addView(view);
                    ImageInfo info = new ImageInfo();
                    info.setThumbnailUrl(JointImageUrl.smallImageUrl(vo.getReplyImgs().get(i).getImgID()));
                    info.setBigImageUrl(JointImageUrl.bigImageUrl(vo.getReplyImgs().get(i).getImgID()));
                    imageInfoRep.add(info);
                }
                nineGridRep.setAdapter(new NineGridViewClickAdapter(this, imageInfoRep));
            }
        }

    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                switch (msg.what) {
                    case MSG.MSG_GET_FB_INFO_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_FB_INFO_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {

        vo = FastJsonUtil.getBean(object.toString(),"feedBack",MyFeedbackListVO.class);

        setView();

    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
